import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/9 9:28
 * @description 二分查找，left = 1, right = max(arr[i]), 那么假设最大为y, 每个大于y的袋子都需要拆分，拆分次数 (arr[i] - 1) / y, 那么总共拆分次数 + <= max,
 * 说明y还可以取更小 r = mid - 1, 否则 l = mid
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {7,17};
        solution.minimumSize(nums, 2);
    }
    public int minimumSize(int[] nums, int maxOperations) {
        int left = 1, right = 0;
        for (int a: nums) {
            if (a > right) {
                right = a;
            }
        }
        int ans = 0;
        while (left <= right) {
            int mid = (left + right) /2;
            if (check(nums, mid, maxOperations)) {
                ans = mid;
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return ans;
    }
    public boolean check(int[] nums, int x, int maxOperations) {
        for (int a: nums) {
            if (a > x) {
                maxOperations -= (a - 1) / x;
                if (maxOperations < 0) {
                    return false;
                }
            }
        }
        return true;
    }
}
